package com.walkline.screen;

import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.browser.field2.BrowserFieldConfig;
import net.rim.device.api.browser.field2.BrowserFieldListener;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.FullScreen;
import net.rim.device.api.ui.container.MainScreen;

import org.w3c.dom.Document;

import com.walkline.app.TravelAssistantAppConfig;
import com.walkline.travelassistant.TravelAssistantConfig;
import com.walkline.util.network.MyConnectionFactory;

public final class ShowStaticImageScreen extends MainScreen
{
	private BrowserFieldConfig bfc;
	private BrowserField _browserField;
	private FullScreen _main;

    public ShowStaticImageScreen(final String path)
    {
    	super (VERTICAL_SCROLL | HORIZONTAL_SCROLL);

    	_main = this;

    	bfc = new BrowserFieldConfig();
		bfc.setProperty(BrowserFieldConfig.ALLOW_CS_XHR, Boolean.TRUE);
		//bfc.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE);
		bfc.setProperty(BrowserFieldConfig.USER_SCALABLE, Boolean.TRUE);
		bfc.setProperty(BrowserFieldConfig.NAVIGATION_MODE, BrowserFieldConfig.NAVIGATION_MODE_POINTER);
		bfc.setProperty(BrowserFieldConfig.VIEWPORT_WIDTH, new Integer(Display.getWidth()));
		bfc.setProperty(BrowserFieldConfig.CONNECTION_FACTORY, new MyConnectionFactory());

		_browserField = new BrowserField(bfc);
		_browserField.addListener(new MyBrowserFieldListener());

		UiApplication.getUiApplication().invokeLater(new Runnable()
		{
			public void run()
			{
				_browserField.requestContent(TravelAssistantConfig.queryStaticImage + 
																	"?center=" + path +
																	"&width=" + Display.getWidth() * 1.5 +
																	"&height=" + Display.getHeight() * 1.5 +
																	//"&scale=1" +
																	"&zoom=17");			
			}
		});
    }

    private void showContent()
	{
		if (_browserField.getScreen() == null)
		{
			synchronized (UiApplication.getEventLock())
			{
				_main.add(_browserField);
			}
		}
	}

	private class MyBrowserFieldListener extends BrowserFieldListener
	{
		public void documentLoaded(BrowserField browserField, Document document) throws Exception
		{
			showContent();
		}

		public void documentAborted(BrowserField browserField, Document document) throws Exception {}
		public void documentError(BrowserField browserField, Document document) throws Exception {}
		public void documentUnloading(BrowserField browserField, Document document) throws Exception {}
	}
}